<template>
  <div class="good-list">
    <!-- 筛选面板 -->
    <el-row :gutter="20">
      <el-col :span="5">
        <el-input v-model="Guanjianzi" placeholder="请输入关键字" />
      </el-col>
      <el-col :span="5">
        <el-input v-model="Lianjie" placeholder="请输入跳转链接" />
      </el-col>
      <el-col :span="2">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查找</el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" icon="el-icon-edit" @click="addData">添加</el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table id="tableId" :data="tableData" border style="width: 100%">
      <el-table-column align="center" prop="id" label="关键词ID" sortable />
      <el-table-column align="center" prop="keyword" label="关键词" />
      <el-table-column align="center" prop="url" width="450" label="跳转链接" />
      <el-table-column align="center" label="是否推荐">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHot === true ? 'success' : 'primary'">{{ scope.row.isHot === true ? '是' : '否'
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否默认">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDefault === true ? 'success' : 'primary'">{{ scope.row.isDefault === true ? '是'
              : '否'
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item :required="true" label="关键词" :label-width="formLabelWidth">
          <el-input v-model="form.keyword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="跳转链接" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否推荐" :label-width="formLabelWidth">
          <el-select v-model="form.isHot" placeholder="请选择">
            <el-option v-for="item in hott" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否默认" :label-width="formLabelWidth">
          <el-select v-model="form.isDefault" placeholder="请选择">
            <el-option v-for="item in defaultt" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn()">取 消</el-button>
        <el-button type="primary" @click="agreeBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination style="margin-top: 30px" :current-page="currentPage" :page-sizes="[5, 10, 20]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />

  </div>
</template>
<script>
import { Row } from 'element-ui'
import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'
import { keywordList, keywordUpdate, keywordCreate, keywordDelete } from '../../api/ml-user'

export default {
  name: 'GuanJianCi',
  props: [],
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      Guanjianzi: '',
      Lianjie: '',
      tableData: [],
      total: 0, // 总条数
      currentPage: 1, // 当前页码
      pageSize: 20, // 每页的数据条数
      sort: 'add_time',
      order: 'desc',
      dialogFormVisible: false,
      form: {},
      delD: {},
      formLabelWidth: '120px',
      isEdit: false,
      hott: [{
        value: true,
        label: '推荐'
      }, {
        value: false,
        label: '普通'
      }],
      defaultt: [{
        value: true,
        label: '默认'
      }, {
        value: false,
        label: '非默认'
      }],
      // value1: '',
      // value2: '',
    }
  },
  created() {
    this.getData(this.currentPage, this.pageSize, this.sort, this.order)
  },
  methods: {
    // 弹出框————————————————————————————————
    // 取消
    cancelBtn() {
      this.isEdit = false;
      this.dialogFormVisible = false
      this.form = {}
    },
    // 确认
    agreeBtn() {
      this.dialogFormVisible = false;
      // 如果是编辑
      if (this.isEdit) {
        // this.dialogFormVisible = false;
        // console.log(this.form);
        keywordUpdate(this.form).then(res => {
          console.log(res);
        })
        // 如果是添加
      } else {
        // { desc: this.form.desc, floorPrice: this.form.floorPrice, name: this.form.name }
        keywordCreate(this.form).then(res => {
          // console.log(res);
          if (res.errno == 0) {
            this.getData()
          }
        })
      }
      this.isEdit = false;
      this.form = {}
    },
    // 编辑
    handleEdit(index, row) {
      // console.log(index);
      this.isEdit = true
      console.log(row);
      this.form = row
      // this.value1 = row.isHot
      // this.value2 = row.isDefault
      this.dialogFormVisible = true
    },
    // 删除
    handleDelete(index, row) {
      // console.log(index);
      // console.log(row);
      this.delD.id = row.id;
      this.delD.addTime = row.addTime;
      this.delD.question = row.question;
      this.delD.answer = row.answer;
      this.delD.updateTime = row.updateTime;
      // console.log(this.delD);
      keywordDelete(this.delD).then(res => {
        // console.log(res);
        if (res.errno == 0) {
          this.tableData.splice(index, 1)
        }
      })

    },
    // 添加
    addData() {
      this.dialogFormVisible = true
      this.isEdit = false
    },
    // 获取数据
    getData(n1, n2, n3, n4) {
      keywordList({ page: this.page, limit: this.limit, sort: this.sort, order: this.order, question: this.QuestionContxt }).then(res => {
        console.log(res)
        if (res.errno == 0) {
          this.tableData = res.data.items,
            this.total = res.data.total
        }
      })
    },

    // 点击导出
    handleExport() {
      var xlsxParam = { raw: true }
      const fix = document.querySelector('.el-table__fixed')
      let wb
      // 判断有无fixed定位，如果有的话去掉，后面再加上，不然数据会重复
      if (fix) {
        wb = XLSX.utils.table_to_book(
          document.querySelector('#tableId').removeChild(fix), xlsxParam
        )
        document.querySelector('#tableId').appendChild(fix)
      } else {
        wb = XLSX.utils.table_to_book(document.querySelector('#tableId'), xlsxParam)
      }
      var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream' }),
          '导出详情单.xlsx'
        ) // 文件名
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    },
    // 点击搜索
    handleSearch() {
      // IssueList({ page: n1, limit: n2, sort: n3, order: n4 }).then(res => {
      //   console.log(res);
      // })
      if (this.QuestionContxt != '') {
        var newtabData1 = []
        this.tableData.forEach(item => {
          if (item.question == this.QuestionContxt) {
            newtabData1.push(item)
          }
        })
        this.tableData = newtabData1
      } else {
        this.getData()
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getData(1, val, this.sort, this.order)
      this.currentPage = 1
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getData(this.currentPage, this.pageSize, this.sort, this.order)

    }
  }
}
</script>

<!-- scss -->
<style lang="scss" scoped>
.good-list {
  padding: 20px;
  box-sizing: border-box;

  .el-table {
    margin-top: 20px;

    .pic {
      height: 80px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  width: 178px;
  height: 178px;
  overflow: hidden;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>